//package com.allwees.bs.c.common.auth.config;
//
//import com.allwees.bs.c.common.auth.constant.AuthConstant;
//import com.allwees.bs.c.module.user.security.AuthUser;
//import com.allwees.bs.core.modelbase.constant.ResultCode;
//import com.allwees.core.common.util.DateUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
//import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
//import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
//import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
//import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
//import org.springframework.security.oauth2.provider.ClientDetails;
//import org.springframework.security.oauth2.provider.ClientDetailsService;
//import org.springframework.security.oauth2.provider.client.BaseClientDetails;
//import org.springframework.security.oauth2.provider.client.InMemoryClientDetailsService;
//import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
//import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
//import org.springframework.security.oauth2.provider.token.TokenEnhancer;
//import org.springframework.security.oauth2.provider.token.TokenStore;
//
//import java.util.Date;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * OAuth2授权服务器配置
// * 1、继承AuthorizationServerConfigurerAdapter
// * 2、注解@EnableAuthorizationServer 启用授权服务器
// *
// */
//@Slf4j
//@Configuration
//@EnableAuthorizationServer
//public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
//	/**
//	 * Token有效期：30天，0表示不失效
//	 */
//	static final int ACCESS_TOKEN_VALIDITY_SECONDS = 60 * 60 * 24 * 30;
//
//	static final int FREFRESH_TOKEN_VALIDITY_SECONDS = 60 * 60 * 6;
//
//	@Autowired
//	private TokenStore tokenStore;
//
//	@Autowired
//	private AuthenticationManager authenticationManager;
//
//	@Override
//	public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {
//		configurer.withClientDetails(getClientDetailsService());
//	}
//
//	@Override
//	public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
//		endpoints.reuseRefreshTokens(true)
//				.tokenServices(getTokenServices())
//				.authenticationManager(authenticationManager);
//	}
//
//	@Bean
//	@Primary
//	AuthorizationServerTokenServices getTokenServices() {
//		DefaultTokenServices tokenServices = new DefaultTokenServices();
//		tokenServices.setTokenStore(tokenStore);
//		tokenServices.setSupportRefreshToken(true);
//		tokenServices.setReuseRefreshToken(true);
//		tokenServices.setRefreshTokenValiditySeconds(FREFRESH_TOKEN_VALIDITY_SECONDS);
//		tokenServices.setAccessTokenValiditySeconds(ACCESS_TOKEN_VALIDITY_SECONDS);
//		tokenServices.setClientDetailsService(getClientDetailsService());
//		tokenServices.setTokenEnhancer(tokenEnhancer());
//		return tokenServices;
//	}
//
//	@Bean
//	ClientDetailsService getClientDetailsService() {
//		InMemoryClientDetailsService clientDetailsService = new InMemoryClientDetailsService();
//		clientDetailsService.setClientDetailsStore(clientDetailsStore());
//		return clientDetailsService;
//	}
//
//	private Map<String, ClientDetails> clientDetailsStore() {
//		BaseClientDetails clientDetails = new BaseClientDetails(AuthConstant.CLIEN_ID, null, AuthConstant.SCOPE, AuthConstant.GRANT_TYPES, null);
//		clientDetails.setClientSecret(AuthConstant.CLIENT_SECRET);
//		Map<String, ClientDetails> hashMap = new HashMap<>();
//		hashMap.put(AuthConstant.CLIEN_ID, clientDetails);
//		return hashMap;
//	}
//
//	private TokenEnhancer tokenEnhancer() {
//		return (accessToken, authentication) -> {
//			final Map<String, Object> additionalInfo = new HashMap<>(8);
//			AuthUser user = (AuthUser) authentication.getUserAuthentication().getPrincipal();
//			additionalInfo.put("userUuid", user.getUserUuid());
//			additionalInfo.put("username", user.getUsername());
//			additionalInfo.put("code", ResultCode.SUCCESS);
//			DefaultOAuth2AccessToken oAuth2AccessToken = ((DefaultOAuth2AccessToken) accessToken);
//			oAuth2AccessToken.setExpiration(DateUtil.addDays(new Date(), 30)); //过期时间30天
//			oAuth2AccessToken.setAdditionalInformation(additionalInfo);
//			return accessToken;
//		};
//	}
//}
